package com.bank.service;

import com.bank.model.ReturnMessage;
import com.bank.utils.CRUD;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
public class BuyService {
    @Autowired
    CRUD op;

    public ReturnMessage buyProduct(String uid,String pid,String num,String price_s,String cid){
        BigDecimal count = null;
        BigDecimal price = null;

        try {
            count = new BigDecimal(num);
            price = new BigDecimal(price_s);
            if(count.compareTo(new BigDecimal("0")) <= 0){
                return new ReturnMessage(0,"购买数量必须大于0");
            }
        }catch (Exception e){
            e.printStackTrace();
            return new ReturnMessage(0,"输入格式错误");
        }

        switch (pid.substring(18)){
            case "11":
                //股票
                String sto[] = num.split("\\.");
                if(sto.length == 2){
                    return new ReturnMessage(0,"股票购买数量必须为整数");
                }
                break;
            case "22":
                //基金
                String fun[] = num.split("\\.");
                if(fun.length == 2 && fun[1].length() > 2){
                    return new ReturnMessage(0,"基金购买份额最多输入两位小数");
                }
                break;
            case "33" :
                //保险
                String ins[] = num.split("\\.");
                if(ins.length == 2){
                    return new ReturnMessage(0,"保险购买数量必须为整数");
                }
                break;
            default:
                return new ReturnMessage(0,"理财产品编号错误");
        }


        return op.buyProduct(uid,pid,count,price,cid);
    }

}
